<template>
  <div class="login-container">
    <div class="login-bg">
      <!-- 商城LOGO -->
      <div class="logo-container">
        <img src="@/assets/images/logo.jpg" alt="全都有超市" class="logo-image">
        <h1 class="logo-text">全都有超市</h1>
      </div>

      <h2 class="login-title">欢迎回来</h2>
      <p class="login-desc">请登录以继续您的购物体验</p>

      <!-- 登录表单 -->
      <el-form ref="form" :model="loginForm" label-width="80px" class="login-form">
        <el-form-item label="用户名">
          <el-input v-model="loginForm.username" prefix-icon="user"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="loginForm.password" show-password prefix-icon="lock"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" class="login-btn" :loading="loading">
            <i class="el-icon-shopping-cart-2"></i>
            立即登录
          </el-button>
        </el-form-item>
      </el-form>

      <div class="login-extra">
        <a @click="resetPassword">忘记密码？</a>
        <span>没有账号？<a @click="goToRegister">注册</a></span>
      </div>
    </div>
  </div>
</template>
<script>
import api from '@/api/login'
import router from '@/router'

export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loading: false
    };
  },
  mounted(){
    document.addEventListener('keydown',this.handleKeyDown)
  },
  beforeUnmount(){
    document.removeEventListener('keydown',this.handleKeyDown)
  },
  methods: {
    async resetPassword(){
      const username = this.loginForm.username
      router.push({
        name:'reset',
        params:username ? {username} : {}
    
      })
    },
    async goToRegister(){
      router.push({
        name:'register',
        params:{
          username: this.loginForm.username
        }
      })
    },
    async onSubmit() {
  this.loading = true;

  try {
    const { username, password } = this.loginForm;

    if (!username || !password) {
      this.$message.error('请输入用户名和密码');
      return;
    }

    // 执行登录
    const loginResponse = await api.login({
      username,
      password
    });

    if (loginResponse.data.code === 200 && loginResponse.data.data === true) {
      this.$message.success('登录成功');

      // 获取用户职位（由你编写具体的判断逻辑）
      const positionResponse = await api.getPosition(username);
      let position = null;
      if (positionResponse.data.code === 200) {
        position = positionResponse.data.data.pName;
      } else {
        this.$message.error('无法获取用户职位信息');
      }

      // 将用户信息保存到 Vuex
      this.$store.dispatch('saveUserInfo', {
        username,
        password,
        position
      });
      console.log('用户信息保存成功:', this.$store.getters.getUserInfo);

      // 根据职位跳转页面
      if (position === '用户') {
        router.replace('/home');
      } else {
        router.replace('/admin');
      }
    } else {
      this.$message.error(loginResponse.data.message || '用户名或密码错误');
    }
  } catch (error) {
    console.error('登录失败:', error);
  } finally {
    this.loading = false;
  }
},
    
  handlePositionCheck(position, username) {

    if (!position) {
      // 如果没有获取到职位信息，默认跳转到用户界面
      router.push('/');
      return;
    }
      
      // 调用你自己的判断逻辑
    this.customPositionHandler(position, username);
  },
  handleKeyDown(e) {
  const target = e.target
  if ((e.key === 'Enter' || e.keyCode === 13) &&
      (target.tagName === 'INPUT' && (target.type === 'text' || target.type === 'password'))) {
    this.onSubmit()
  }
}
  }
}
</script>

<style lang="less" scoped>
@primary-color: #ff4d4f;
@text-light: #666;
@bg-gray: #f8f9fa;
@border-radius: 8px;

.login-container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat center center fixed;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.login-bg {
  width: 400px;
  padding: 40px 30px;
  border-radius: @border-radius;
  background-color: rgba(255, 255, 255, 0.9); // 添加透明度
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.1);
  text-align: center;
  backdrop-filter: blur(10px);
}

.logo-container {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 30px;

  .logo-image {
    width: 40px;
    height: 40px;
    margin-right: 10px;
    border-radius: 50%;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  }

  .logo-text {
    font-size: 20px;
    color: @primary-color;
    font-weight: bold;
    letter-spacing: 1px;
    margin: 0;
  }
}

.login-title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.login-desc {
  font-size: 14px;
  color: #666;
  margin-bottom: 30px;
}

.login-form {
  width: 100%;
  margin-bottom: 20px;

  .el-form-item {
    margin-bottom: 24px;

    &:last-child {
      margin-bottom: 0;
    }
  }

  .login-btn {
    width: 100%;
    background-color: @primary-color;
    border-color: @primary-color;
    font-size: 16px;
    font-weight: 500;
    transition: all 0.3s ease;
    border-radius: @border-radius;

    &:hover {
      background-color: darken(@primary-color, 10%);
      border-color: darken(@primary-color, 10%);
    }

    i {
      margin-right: 8px;
    }
  }
}

.login-extra {
  display: flex;
  justify-content: space-between;
  width: 100%;
  font-size: 13px;
  color: #999;

  a {
    color: @primary-color;
    text-decoration: none;
    margin: 0 5px;

    &:hover {
      text-decoration: underline;
    }
  }
}
</style>